<template>
<div>
    <Form :model="formCustom" :label-width="80">
        <FormItem label="名称" prop="topic">
            <Input type="text" v-model="formCustom.topic" ></Input>
        </FormItem>
        <FormItem label="图片" prop="head" >
            <Upload action="//upload.qiniup.com/" 
            :data="token"
            :format="['jpg','jpeg','png']" 
            :on-success="suc">
                <img :src="formCustom.head"/> 
            </Upload>
        </FormItem>
        <FormItem label="链接" prop="redirect_url">
            <Input type="text" v-model="formCustom.redirect_url" ></Input>
        </FormItem>
        <FormItem label="排序" prop="sort">
            <Input type="text" v-model="formCustom.sort" ></Input>
        </FormItem>
        <FormItem>
            <Button type="primary" @click="handleSubmit">Submit</Button>
        </FormItem>
    </Form>
</div>
</template>
<script>
    import util from '../libs/util';
    export default {
        created() {
           this.formCustom = this.$route.query;
           this.loadUpToken();
        },
        data () {
            return {
                formCustom : {},
                token: {}
            }
        },
        methods: {
            suc(res, file) {
// 上传成功，图片赋值
                this.formCustom.head = res.baseUrl+"/"+res.key;
                //console.log(this.formCustom.head);
            },

            // 加载上传token
            loadUpToken() {
                this.$http.get(util.baseURL+"qiniu/getUpToken",
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               this.token = {
                                   token : response.data.data
                               }
                            } 
                        },
                        (error)=>{
                            this.$Message.error("更新失败！");
                        }
                );
                this.token = {
                    token:''
                }
            },

            handleSubmit () {
               // this.formCustom.head = "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2176610467,2174515257&fm=27&gp=0.jpg",
                this.$http.post(util.baseURL+"ads/editAds",this.formCustom,
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                               this.$Message.info("更新成功！");
                               this.$router.push("/index/advertisement");
                               this.formCustom = {};
                            } else {
                               this.$Message.error("更新失败！");
                            }
                        },
                        (error)=>{
                            this.$Message.error("更新失败！");
                        }
                );
            },
        }
    }
</script>